<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
    <title>Cindy JS</title>
    <script type="text/javascript" src="../../build/js/Cindy.js"></script>
    <script type="text/javascript" src="../../build/js/CindyGL.js"></script>
    <link rel="stylesheet" href="../../css/cindy.css">
  </head>
    
	<body style="font-family:Arial;">
    
    <h1>CindyJS: Tilings for 3 Generator Coxeter Groups</h1>
    
               
    <script id="csinit" type="text/x-cindyscript">
      use("CindyGL");
      

      init(p, q, r) := (
        sign(a) := if(a>0, 1, if(a<0,-1,0));
        
        spaceType=(sign(q*r+p*r+p*q-p*q*r));//1/p+1/q+1/r-1;

        tmp = (cos(pi/q)+cos(pi/p)*cos(pi/r))/sin(pi/p);
        
        na=(1,0,0);
        nb=(-cos(pi/p), sin(pi/p),0.);
        nc=(-cos(pi/r),-tmp,if(spaceType==0, 0.25, sqrt(abs((tmp+sin(pi/r))*(-tmp+sin(pi/r))))));
      );
  
      reflectIfNegative(pos, n) := ( //reflectIfNegatives pos along n if necessary
        pos -2*min(0., n*pos)*(n.x,n.y,n.z*spaceType)
      );
      
      fold(p) := (
        repeat(20,
          p = reflectIfNegative(reflectIfNegative(reflectIfNegative(p,
            na),
            nb),
            nc);
        );
        p
      );
      
      embed(pos) := ( //embed in sphere, RP2, hyperbolid
        r2 = pos*pos;
        if(spaceType==1,
          (pos.x, pos.y, re(sqrt(1-r2))),
        if(spaceType==0,
          (pos.x, pos.y, 1),
        //spaceType==-1
          (2*pos.x, 2*pos.y, 1+r2)/(1-r2) //hyperbolid -x*x-y*y+z*z=1
        ))
      );
      
      project(p) := ( //project from sphere, RP2, hyperbolid back to original space
        if(spaceType==1,
          (p.x, p.y)/(abs(p)),
        if(spaceType==0,
          (p.x, p.y)/(p.z),
        //spaceType ==-1
          p = p/(sqrt(p.z*p.z-p.y*p.y-p.x*p.x));
          (p.x, p.y)/(1+p.z)
        ))
      );
      
      color(pos) := (
        p = embed(pos);
        r2 = pos*pos;
        if(abs(spaceType) == 1 & r2>=1.,
          gray(1.), //outside poincare disc or projected circle
          fp = fold(p); //corresponding point in fundamental cell
          
          //hue(.3*t+fp.x+fp.y)
          (.5+.5*sin(.7*t+fp.x*5),.5+.5*sin(.3*t+fp.y*5),.5+.5*sin(.8*t+fp.x*4+fp.z*2+fp.y*3)) //some fancy color
          *(.7*min(p.z,1)+.3)  //looks a bit more sphere like
          *if(showfundamentaldomain & na*p>0 & nb*p>0 & nc*p>0, .7, 1)
        )
      );
      
      
      init(2,3,6);
      t0 = seconds();
      showfundamentaldomain = false;
      
      //supersampling
      createimage("image", 1000, 1000);
    </script>
    
    <script id="csmousedown" type="text/x-cindyscript">
      showfundamentaldomain = true;
    </script>
    <script id="csmouseup" type="text/x-cindyscript">
      showfundamentaldomain = false;
    </script>
    
    <script id="csmove" type="text/x-cindyscript">
      
        draw((1.1,.1),(1.1,.8),color->(0,0,0),size->2);
        draw((1.2,.1),(1.2,.8),color->(0,0,0),size->2);
        draw((1.3,.1),(1.3,.8),color->(0,0,0),size->2);
        y=CA.y;y=round(min((max((y,.1)),.8))*10)/10;CA.xy=(1.1,y);
        y=CB.y;y=round(min((max((y,.1)),.8))*10)/10;CB.xy=(1.2,y);
        y=CC.y;y=round(min((max((y,.1)),.8))*10)/10;CC.xy=(1.3,y);

        pa=CA.y*10+1;
        pb=CB.y*10+1;
        pc=CC.y*10+1;
        
        p = round(pa);
        q = round(pb);
        r = round(pc);
        drawtext(CA+(0.02,0.02),round(pa));
        drawtext(CB+(0.02,0.02),round(pb));
        drawtext(CC+(0.02,0.02),round(pc));

      init(p, q, r);
    </script>
    
    <script id="csdraw" type="text/x-cindyscript">
      t = seconds()-t0;
      //draw((0,0), color->(color((1,1))));
      
      colorplot([-1,-1], [1,-1], "image", color(#));
      drawimage([-1,-1], [1,-1], "image");
      
      if(showfundamentaldomain,
        draw(project(cross(na,nb)), color->(1,0,0));
        draw(project(cross(nb,nc)), color->(0,1,0));
        draw(project(cross(nc,na)), color->(0,0,1));
      );
    </script>

    <div  id="CSCanvas"></div>
    <script type="text/javascript">
        
        cdy = CindyJS({canvasname:"CSCanvas",
                    scripts: "cs*",
                    geometry: [
                      
    {name:"CA", type:"Free", pos:[1.2,.1],color:[1,0,0],pinned:false,size:6},
    {name:"CB", type:"Free", pos:[1.3,.2],color:[0,1,0],pinned:false,size:6},
    {name:"CC", type:"Free", pos:[1.3,.5],color:[0,0,1],pinned:false,size:6},
                    ],
                    animation: {autoplay: true},
                    ports: [{
                      id: "CSCanvas",
                      width: 600,
                      height: 500,
                      transform: [ { visibleRect: [-1, -1, 1.4, 1] } ]
                    }]
                  });
    </script>              
	</body>
</html>
